﻿using System;
using System.Collections.Generic;

using System.Xml;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;



namespace XGame
{

    /// <summary>
    /// The type of Collisions that the object will have
    /// </summary>
    public enum CollisonType
    {
        PerPixel,
        BoundingBox,
        None
    };
    
    public class Sprite:DrawableGameComponent
    {
       
      protected Texture2D sprite;
      protected Vector2 position;

      public Vector2 Position
      {
          get { return position; }
          set { position = value; }
      }

      protected Vector2 origin;
      public Vector2 Origin
      {
          get { return origin; }
          set { origin = value; }
      }

      protected float height;
      public float Height
      {
          get { return height; }
      }

      protected float width;
      public float Width
      {
          get { return width; }
      }

      protected float rotation;

      public float Rotation
      {
          get { return rotation; }
          set { rotation = value; }
      }

      protected float scale;

      public float Scale
      {
          get { return scale; }
          set {scale = value;}
      }

      protected float scaleX;

      public float ScaleX
      {
          get { return scaleX; }
          set { scaleX = value; }
      }

      protected float scaleY;

      public float ScaleY
      {
          get { return scaleY;}
          set { scaleY = value; }
      }
      protected bool canDraw = false;

      public bool CanDraw
      {
          get { return canDraw; }
          set { canDraw = value; }
      }

      //Collisison stuff
      public Rectangle collisionRect;


      //rectangles for drawing stuff
      Rectangle sourceRect;
      public Rectangle SourceRect
      {
          get { return sourceRect; }
          set { sourceRect = value; } 
      } //this is where we get the texture from
      Rectangle destinationRect;
      //this is where we draw it too
      public Rectangle DestinationRect 
      { 
          get { return destinationRect; }
          set { destinationRect = value; } 
      }

      protected Color renderColor;
      public Color RenderColor
      {
          get { return renderColor; }
          set { renderColor = value; }
      }


      public CollisonType CollisionType;
      public Color[] collisionData;
      public Matrix transformMatrix;
      
      

      
       public Sprite(Texture2D Sprite, Game gameRef, CollisonType CollisionType):base(gameRef)
       {
           
            this.CollisionType = CollisionType;
            sprite = Sprite;
            height = Sprite.Height;
            width = Sprite.Width;
            origin = new Vector2(sprite.Width / 2, sprite.Height / 2);
            collisionData = new Color[sprite.Width * sprite.Height];
            sprite.GetData(collisionData);
            sourceRect = new Rectangle(0,0,sprite.Width,sprite.Height);
            destinationRect = new Rectangle(0, 0, sprite.Width, sprite.Height);
                      
           
       }

       public Sprite(Texture2D Sprite, Game gameRef):base( gameRef)
           
       {
           CollisionType = CollisonType.None;
           sprite = Sprite;
           height = Sprite.Height;
           width = Sprite.Width;
           origin = new Vector2(sprite.Width / 2, sprite.Height / 2);
           collisionData = new Color[sprite.Width * sprite.Height];
           sprite.GetData(collisionData);
           sourceRect = new Rectangle(0, 0, sprite.Width, sprite.Height);
           destinationRect = new Rectangle(0, 0, sprite.Width, sprite.Height);

           
       }

        /// <summary>
        /// This is the constructor used in the editor becasue there is no Game for the object to refrence
        /// </summary>
        /// <param name="Sprite"></param>
       public Sprite(Texture2D Sprite)
           : base(null)
       {
           CollisionType = CollisonType.None;
           sprite = Sprite;
           height = Sprite.Height;
           width = Sprite.Width;
           origin = new Vector2(sprite.Width / 2, sprite.Height / 2);
           collisionData = new Color[sprite.Width * sprite.Height];
           sprite.GetData(collisionData);
           sourceRect = new Rectangle(0, 0, sprite.Width, sprite.Height);
           destinationRect = new Rectangle(0,0,sprite.Width,sprite.Height);
           renderColor = Color.White;

       }

       public Sprite(Game gameRef):base(gameRef)
       {
  
       }

       public void Update()
       {
           //create the transform matrix

           
               //transformMatrix = Matrix.CreateTranslation(new Vector3(-center, 0.0f)) *
               //         //Matrix.CreateScale(block.Scale) *  //would go here
               //         Matrix.CreateRotationZ(rotation) *
               //         Matrix.CreateTranslation(new Vector3(position, 0.0f));

               //collisionRect = HitTest.CalculateBoundingRectangle(
               //              new Rectangle(0, 0,(int)width, (int)height),
               //              transformMatrix);
           


           
       }

       public void serailize()
       {
          
       }



       public virtual void Draw(SpriteBatch spriteBatch)
       {
           spriteBatch.Draw(this.sprite, this.DestinationRect,this.sourceRect, renderColor, rotation, origin, SpriteEffects.None,0.0f);
       }

       

       public void setSourceWidth(int newWidth, bool resizeDestination)
       {
           sourceRect.Width = newWidth;
           if (resizeDestination)
               setDestinationWidth(newWidth,false);
       }
       public void setSourceHeight(int newHeight, bool resizeDestination)
       {
           sourceRect.Height = newHeight;
           if (resizeDestination)
               setDestinationHeight(newHeight, false);
       }

        //Do you really need the resize bit?
       public void setDestinationWidth(int newWidth, bool resizeSource)
       {
           destinationRect.Width = newWidth;
           if (resizeSource)
               setSourceWidth(newWidth, false);
       }
       public void setDestinationHeight(int newHeight, bool resizeSource)
       {
           destinationRect.Height = newHeight;
           if (resizeSource)
               setSourceHeight(newHeight, false);
       }

       public void setDestinationScaleX(float scale)
       {
           destinationRect.Width = (int)(sourceRect.Width * scale);
       }

       public void setDestinationScaleY(float scale)
       {
           destinationRect.Height = (int)(sourceRect.Height * scale);
       }

       public void setSourceRectPosition(Vector2 newPosition)
       {
           sourceRect.X = (int) newPosition.X;
           sourceRect.Y = (int) newPosition.Y;
       }

       public void setDesitinationRectPosition(Vector2 newPosition)
       {
           destinationRect.X = (int)newPosition.X;
           destinationRect.Y = (int)newPosition.Y;
       }
    }
}
